package com.example.lianjuan.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.lianjuan.Base.IBaseInfo;
import com.example.lianjuan.Model.domain.OnSellPager;
import com.example.lianjuan.R;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.UtilsUrl;

import java.util.ArrayList;
import java.util.List;

public class OnSellContentAdapter extends RecyclerView.Adapter<OnSellContentAdapter.InnerHolder> {
    private List<OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> data = new ArrayList<>();
    private ItemClickListener itemClickListener;

    @NonNull
    @Override
    public OnSellContentAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.on_sell_recycler, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull OnSellContentAdapter.InnerHolder holder, int position) {
        OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO mapData = data.get(position);
        holder.setView(mapData);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(mapData);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setData(List<OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> mapData) {
        this.data.clear();
        this.data.addAll(mapData);
        notifyItemRangeChanged(0, this.data.size());
    }

    public void setMoreData(List<OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO> data) {
        int originalSize = this.data.size();
        this.data.addAll(data);
        notifyItemRangeChanged(originalSize, this.data.size());
        LogUtils.d(this, "data--->" + originalSize + "     this.data---->" + this.data.size());
    }


    public class InnerHolder extends RecyclerView.ViewHolder {
        private TextView preferentialPrice, originalPrice, title;
        private ImageView pagerPicture;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            preferentialPrice = itemView.findViewById(R.id.on_sell_preferential_price);
            originalPrice = itemView.findViewById(R.id.on_sell_original_price);
            title = itemView.findViewById(R.id.on_sell_title);
            pagerPicture = itemView.findViewById(R.id.on_sell_pager);
        }

        public void setView(OnSellPager.DataDTO.TbkDgOptimusMaterialResponseDTO.ResultListDTO.MapDataDTO mapData) {
            //优惠链接
            String coverPath = UtilsUrl.getCoverPath(mapData.getPict_url(), 160);
            LogUtils.d(OnSellContentAdapter.this, "url---->" + coverPath);
            Glide.with(itemView.getContext()).load(coverPath).into(pagerPicture);
            //商品title
            title.setText(mapData.getTitle());
            //商品原价
            Float original = Float.parseFloat(mapData.getZk_final_price());
            originalPrice.setText(String.format(itemView.getContext().getString(R.string.on_sell_preferential_price), original));
            originalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            //商品优惠后的价格
            int CouponAmount = mapData.getCoupon_amount();
            Float finalPrice = original - CouponAmount;
            preferentialPrice.setText(String.format(itemView.getContext().getString(R.string.on_sell_preferential_price), finalPrice));
        }
    }

    public void setOnItemClick(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(IBaseInfo iBaseInfo);
    }
}
